<?php
require_once('include/class.php');
require_once('include/qq_file_uploader.php');
$file_catalog_id=isset($_REQUEST['catalog_id'])?$_REQUEST['catalog_id']:18;
$allowedExtensions = array("jpg", "xml", "bmp",'gif');
$sizeLimit = 10 * 1024 * 1024;
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$relativePath='/../file/'.date('Y/m/');
$targetPath = $_SERVER['DOCUMENT_ROOT'] .$relativePath;
if (!is_dir($targetPath))mkdir(str_replace('//','/',$targetPath), 0777, true);
$result = $uploader->handleUpload($targetPath);
$old_name=$uploader->getName();
$fileExt=strtolower(substr($old_name,strrpos($old_name, '.')+1));
$ranName=$uploader->getUploadName();
$ranName=substr($ranName,0,strrpos($ranName, '.'));
$targetFile =  str_replace('//','/',$targetPath) .$ranName.'.'.$fileExt;
$db=new data();
//echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
//$encrypt=new encrypt();
//$file_id=urlencode($encrypt->encrypt($file_id));
if (isset($result['success'])){
	$file_id=$db->update(array("name"=>$ranName,"catalog_id"=>$file_catalog_id,"original_name"=>$old_name,"time"=>date('Y-m-d h:i:s'),"dir"=>$relativePath,'file_type'=>$fileExt),'file',"insert");
	$p=new CImage($targetFile);        //创建一个图片处理对象
	$p->thumb(str_replace('//','/',$targetPath) .$ranName.'_s.'.$fileExt,160);
	//$p=new CImage($targetFile);        //创建一个图片处理对象
	$p->thumb(str_replace('//','/',$targetPath) .$ranName.'_b.'.$fileExt,600);
	$p->destroy();
	
	echo htmlspecialchars(json_encode(array('success'=>true,'result'=>true,'file_id'=>$file_id,'old_name'=>$old_name)), ENT_NOQUOTES);
}elseif (isset($result['error'])){
	echo htmlspecialchars(json_encode(array('error'=>true,'result'=>false,'error'=>$result['error'])), ENT_NOQUOTES);
}
//echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
die();

	print_r($_FILES);
	print_r($_REQUEST);
if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$oriFile=$_FILES['Filedata']['name'];
	//$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
	$relativePath='/../file/'.date('Y/m/');
	$targetPath = $_SERVER['DOCUMENT_ROOT'] .$relativePath;
	$fileExt=strtolower(substr($oriFile,strrpos($oriFile, '.')+1));
	$file_catalog_id=($fileExt=='jpg'||$fileExt=='gif'||$fileExt=='png')?18:19;
	$file_catalog_id=$_REQUEST['catalog_id'];
	$ranName = date('His').rand (10000000,99999999); 
	$targetFile =  str_replace('//','/',$targetPath) .$ranName.'.'.$fileExt;
//	 $fileTypes  = str_replace('*.','',$_REQUEST['fileext']);
//	 $fileTypes  = str_replace(';','|',$fileTypes);
//	 echo $_REQUEST['fileext'];
	// $typesArray = split('\|',$fileTypes);
	// $fileParts  = pathinfo($_FILES['Filedata']['name']);
	
	if (!is_dir($targetPath))mkdir(str_replace('//','/',$targetPath), 0777, true);
	// if (in_array($fileParts['extension'],$typesArray)) {
		// Uncomment the following line if you want to make the directory if it doesn't exist
		// mkdir(str_replace('//','/',$targetPath), 0755, true);
		
		move_uploaded_file($tempFile,$targetFile);
		$db=new data();
		$file_id=$db->update(array("name"=>$ranName,"catalog_id"=>$file_catalog_id,"original_name"=>$oriFile,"time"=>date('Y-m-d h:i:s'),"dir"=>$relativePath,'file_type'=>$fileExt),'file',"insert");
		
		if ($file_catalog_id==18){
			$p=new CImage($targetFile);        //创建一个图片处理对象
			$p->thumb(str_replace('//','/',$targetPath) .$ranName.'_s.'.$fileExt,160);
			//$p=new CImage($targetFile);        //创建一个图片处理对象
			$p->thumb(str_replace('//','/',$targetPath) .$ranName.'_b.'.$fileExt,600);
			$p->destroy();
		}
		//echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
		$encrypt=new encrypt();
		$file_id=urlencode($encrypt->encrypt($file_id));
		
		echo $file_id.'|'.$file_catalog_id;
	// } else {
	// 	echo 'Invalid file type.';
	// }
}
?>
